//
//  NSVipFuncsView.m
//  Meboo
//
//  Created by kyo on 2024/5/6.
//

#import "NSVipFuncsView.h"
#import "NSVipItemView.h"
#import "NSVipFuncItemView.h"
//#import "KLPaymentManager.h"
#import "MBWebViewController.h"
#import "NSProductModel.h"
//#import "NSVipBuyPopView.h"
@interface NSVipFuncsView ()
@property (nonatomic, strong) UIImageView *FuncsBackImageView;
@property (nonatomic, strong) UIImageView *funcsHeaderImageView;
//@property (nonatomic, strong) NSVipBuyPopView *vipBuyPopView;
@property (nonatomic, strong) NSMutableArray <NSVipItemView *>*viewArr;

//@property (nonatomic , strong) UIButton *buyBtn;
////支付协议
//@property (nonatomic, strong) UILabel *privacyLab0;
////会员服务协议
//@property (nonatomic, strong) UILabel *privacyLab1;
//续订协议
@property (nonatomic, strong) UILabel *privacyLab2;
//product
@property (nonatomic, copy) NSString *productID;
@end

@implementation NSVipFuncsView

#pragma mark - Life Cycle
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setupUI];
//        self.productID = @"meboo_vip_ios_30_days";
        [self getProductData];
    }
    return self;
}



#pragma mark - Action

- (void)getProductData {
    [[MBMineDataManager sharedManager] getMemberProductWithType:11 success:^(BOOL success) {
        if (success) {
            if ([MBMineDataManager sharedManager].vipProductArr) {
                for (UIView *view in self.subviews) {
                    if (view.tag >= 1000) {
                        [view removeFromSuperview];
                    }
                    [self setProductUI];
                    [self setFuncsView];
                }
            }
        }
    }];
}

- (void)setProductUI {
    __WeakSelf
    NSString *price = @"$29.99";
    CGFloat wid = (ScreenWidth-MBWidthScale(60))/3;
    NSInteger num = [MBMineDataManager sharedManager].vipProductArr.count;
    for (int i = 0; i < num; i++) {
        NSVipItemView *view = [NSVipItemView new];
        view.tag = 1000+i;
        NSProductModel *model = [MBMineDataManager sharedManager].vipProductArr[i];
        if (num % 2 == 0) {
            if (i == num/2-1) {
                model.isSelect = YES;
                self.productID = model.productCode;
                price = model.price;
            } else {
                model.isSelect = NO;
            }
        } else {
            if (i == num/2) {
                model.isSelect = YES;
                self.productID = model.productCode;
                price = model.price;
            } else {
                model.isSelect = NO;
            }
        }
        view.model = model;
        view.tag = i;
        view.clickBlock = ^(NSInteger tag) {
            [weakSelf clickItemAction:tag];
//            weakSelf.productID = model.productCode;
//            [weakSelf.buyBtn setTitle:[NSString stringWithFormat:@"$%@ Purchase Now",model.price] forState:0];
            if (weakSelf.selectBlock) {
                weakSelf.selectBlock(model.productCode, model.price);
            }
        };
        [self addSubview:view];
        [view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(MBWidthScale(20) + (MBWidthScale(10)+wid)*i);
            make.width.mas_equalTo(wid);
            make.height.mas_equalTo(MBHeightScale(133));
            make.top.mas_equalTo(MBHeightScale(20));
        }];
        [self.viewArr addObject:view];
    }
    
    if (self.selectBlock) {
        self.selectBlock(self.productID, price);
    }
}

- (void)setFuncsView {
    for (int i = 0; i < [MBMineDataManager sharedManager].vipEquityArr.count; i++) {
        NSVipFuncItemView *view = [NSVipFuncItemView new];
        view.tag = 2000+i;
        view.model = [MBMineDataManager sharedManager].vipEquityArr[i];
        [self addSubview:view];
        [view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self);
            make.height.mas_equalTo(MBHeightScale(70));
            make.top.equalTo(self.funcsHeaderImageView.mas_bottom).offset(MBHeightScale(20)+MBHeightScale(80*i));
            if (i == [MBMineDataManager sharedManager].vipEquityArr.count-1) {
                make.bottom.mas_equalTo(-TabbarSafeBottomMargin-MBHeightScale(35+52));
            }
        }];
        
//        if (i == [MBMineDataManager sharedManager].vipEquityArr.count-1) {
//            [self addSubview:self.buyBtn];
//            [self.buyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//                make.left.mas_equalTo(MBWidthScale(12));
//                make.right.mas_equalTo(MBWidthScale(-12));
//                make.height.mas_equalTo(MBHeightScale(52));
//                make.top.equalTo(view.mas_bottom).offset(MBHeightScale(22));
//                make.bottom.mas_equalTo(-TabbarSafeBottomMargin-MBHeightScale(35));
//            }];
//            
//            [self addSubview:self.privacyLab0];
//            [self.privacyLab0 mas_makeConstraints:^(MASConstraintMaker *make) {
//                make.top.equalTo(self.buyBtn.mas_bottom).offset(MBHeightScale(10));
//                make.left.mas_equalTo(MBWidthScale(20));
//            }];
//            
//            [self addSubview:self.privacyLab1];
//            [self.privacyLab1 mas_makeConstraints:^(MASConstraintMaker *make) {
//                make.top.equalTo(self.privacyLab0);
//                make.left.equalTo(self.privacyLab0.mas_right).offset(15);
//            }];
//        }
    }
}

- (void)clickItemAction:(NSInteger)tag {
    for (int i = 0; i<self.viewArr.count; i++) {
        NSVipItemView *view = self.viewArr[i];
        NSProductModel *model = view.model;
        if (i == tag) {
            model.isSelect = YES;
            self.productID = model.productCode;
        } else {
            model.isSelect = NO;
        }
        view.model = model;
    }
}


//- (void)resumeAction {
//    [[KLPaymentManager sharePaymentManager] restorePurchaase];
//}

//- (void)PurchaseAction {
//    __WeakSelf
//    self.buyBtn.userInteractionEnabled = NO;
//    [[KLPaymentManager sharePaymentManager] payWithProductID:self.productID];
//    [KLPaymentManager sharePaymentManager].payBlock = ^(BOOL payState) {
//        self.buyBtn.userInteractionEnabled = YES;
//        if (payState) {
//            [[MBLoginDataManager sharedManager] getUserInfoWithCode:@"" success:^(BOOL success) {
//                if (weakSelf.paySuccessBlock && success) {
//                    weakSelf.paySuccessBlock();
//                }
//            }];
//            weakSelf.vipBuyPopView.hidden = NO;
//            /**
//             埋点
//             Me非会员点击进入会员订阅页支付成功Me_Nonmember_Premiumsuccess
//             Me会员点击进入会员订阅页换购成功Me_Member_Premiumsuccess
//             */
//            [[NSIOApiManager sharedManager] setIOWithEventName:[MBUserAccount defaultAccount].isVip ? @"Me_Member_Premiumsuccess" : @"Me_Nonmember_Premiumsuccess"];
//        }
//    };
//}

- (void)agreementClick0 {
    MBWebViewController *webVC = [[MBWebViewController alloc] init];
    [webVC loadWithURL:[NSURL URLWithString:paymentAgreementWeb]];
    webVC.titleStr = @"PaidPolicy";
    //    webVC.titleStr = @"PrivacyPolicy";
    [self.currentViewController.navigationController pushViewController:webVC animated:YES];
}

- (void)agreementClick1 {
    MBWebViewController *webVC = [[MBWebViewController alloc] init];
    [webVC loadWithURL:[NSURL URLWithString:MembershipServiceWeb]];
    webVC.titleStr = @"VIP Service Agreement";
    [self.currentViewController.navigationController pushViewController:webVC animated:YES];
}

- (void)agreementClick2 {
    //    MBWebViewController *webVC = [[MBWebViewController alloc] init];
    //    [webVC loadWithURL:[NSURL URLWithString:AutoRenewalAgreementWeb]];
    //    webVC.titleStr = @"About Subscription";
    //    [self.navigationController pushViewController:webVC animated:YES];
}

#pragma mark - UI
- (void)setupUI {
    self.backgroundColor = UIColor.clearColor;
    
    [self addSubview:self.FuncsBackImageView];
    [self.FuncsBackImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
    
    [self addSubview:self.funcsHeaderImageView];
    [self.funcsHeaderImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(MBHeightScale(178));
        make.left.mas_equalTo(MBWidthScale(30));
        make.right.mas_equalTo(MBWidthScale(-30));
        make.height.mas_equalTo((ScreenWidth-MBWidthScale(60))/314*21);
    }];
    
//    [kKeyWindow addSubview:self.vipBuyPopView];
}

#pragma mark - Setter && Getter

- (UIImageView *)FuncsBackImageView {
    if (!_FuncsBackImageView) {
        _FuncsBackImageView = [UIImageView new];
        _FuncsBackImageView.image = ImageWithName(@"mine_vipfucs_back_image");
    }
    return _FuncsBackImageView;
}

- (UIImageView *)funcsHeaderImageView {
    if (!_funcsHeaderImageView) {
        _funcsHeaderImageView = [UIImageView new];
        _funcsHeaderImageView.image = [UIImage imageNamed:@"mine_vipfucs_header_image"];
    }
    return _funcsHeaderImageView;
}

- (NSMutableArray<NSVipItemView *> *)viewArr {
    if (!_viewArr) {
        _viewArr = [NSMutableArray array];
    }
    return _viewArr;
}

//- (UIButton *)buyBtn {
//    if (!_buyBtn) {
//        _buyBtn = [UIButton new];
//        [_buyBtn setBackgroundImage:ImageWithName(@"vip_purchase_back") forState:0];
//        [_buyBtn setTitle:@"$29.99 Purchase Now" forState:0];
//        _buyBtn.titleLabel.font = PDSemiBoldScale(14);
//        [_buyBtn setTitleColor:HEXS(@"ffffff") forState:0];
//        [_buyBtn addTarget:self action:@selector(PurchaseAction) forControlEvents:UIControlEventTouchUpInside];
//    }
//    return _buyBtn;
//}
//
//- (UILabel *)privacyLab0 {
//    if (!_privacyLab0) {
//        _privacyLab0 = [UILabel new];
//        _privacyLab0.userInteractionEnabled = YES;
//        _privacyLab0.font = MBFontSizeScale(10);
//        // 下划线
//        //注意: NSStrikethroughStyleAttributeName 是添加中划线,这时textStr如果是中文字符则没有效果
//        NSDictionary *attribtDic = @{NSUnderlineStyleAttributeName : [NSNumber numberWithInteger:NSUnderlineStyleSingle], NSUnderlineColorAttributeName :HEXS(@"202323")};
//        NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc]initWithString:@"PaidPolicy" attributes:attribtDic];
////        NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc]initWithString:@"PrivacyPolicy" attributes:attribtDic];
//        
//        //赋值
//        _privacyLab0.attributedText = attribtStr;
//        
//        UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(agreementClick0)];
//        
//        [_privacyLab0 addGestureRecognizer:gestureRecognizer];
//    }
//    return _privacyLab0;
//}
//
//- (UILabel *)privacyLab1 {
//    if (!_privacyLab1) {
//        _privacyLab1 = [UILabel new];
//        _privacyLab1.userInteractionEnabled = YES;
//        _privacyLab1.font = MBFontSizeScale(10);
//        // 下划线
//        //注意: NSStrikethroughStyleAttributeName 是添加中划线,这时textStr如果是中文字符则没有效果
//        NSDictionary *attribtDic = @{NSUnderlineStyleAttributeName : [NSNumber numberWithInteger:NSUnderlineStyleSingle], NSUnderlineColorAttributeName :HEXS(@"202323")};
//        NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc]initWithString:@"VIP Service Agreement" attributes:attribtDic];
//        //赋值
//        _privacyLab1.attributedText = attribtStr;
//        
//        UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(agreementClick1)];
//        
//        [_privacyLab1 addGestureRecognizer:gestureRecognizer];
//
//    }
//    return _privacyLab1;
//}

- (UILabel *)privacyLab2 {
    if (!_privacyLab2) {
        _privacyLab2 = [UILabel new];
        _privacyLab2.userInteractionEnabled = YES;
        _privacyLab2.font = MBFontSizeScale(10);
        // 下划线
        //注意: NSStrikethroughStyleAttributeName 是添加中划线,这时textStr如果是中文字符则没有效果
        NSDictionary *attribtDic = @{NSUnderlineStyleAttributeName : [NSNumber numberWithInteger:NSUnderlineStyleSingle], NSUnderlineColorAttributeName :HEXS(@"202323")};
//        NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc]initWithString:@"About Subscription" attributes:attribtDic];
        NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc]initWithString:@"restore purchase" attributes:attribtDic];
        //赋值
        _privacyLab2.attributedText = attribtStr;
        
        UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(agreementClick2)];
        
        [_privacyLab2 addGestureRecognizer:gestureRecognizer];

    }
    return _privacyLab2;
}

//- (NSVipBuyPopView *)vipBuyPopView {
//    if (!_vipBuyPopView) {
//        _vipBuyPopView = [[NSVipBuyPopView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
//    }
//    return _vipBuyPopView;
//}
@end
